<?
$otppasswdFile = '/etc/otppasswd';
$otpstateDir = '/etc/otpstate';
$otptokensFile = '/etc/otptokens';

$radius_clientsFile = '/etc/freeradius/clients.conf';


function showusers() {
	global $otppasswdFile;
	global $otptokensFile;

	echo "<table cellspacing=\"0\"><tr>
		<td class=\"tbhead\">Användarnamn</td>
		<td class=\"tbhead\">Token (EID)</td>
		<td class=\"tbhead\">&nbsp;</td></tr>";

	$otptokens_arr = file($otptokensFile);
	foreach($otptokens_arr AS $otptokens_line) {
		preg_match('/^([^:^\s]*):([^:^\s]*):/', $otptokens_line, $match);
		$tokens[$match[2]] = $match[1];
	}
	

	$otppasswd_arr = file($otppasswdFile);
	$i=0;
 	foreach($otppasswd_arr AS $otppasswd_line) {
		$i++;
		preg_match('/^([^:^\s]*):([^:^\s]*):([^:^\s]*)/', $otppasswd_line, $match);
		$tokens_taken[$tokens[$match[3]]] = true;

		$user = $match[1];
		$eid = $tokens[$match[3]];

		$sync_link = '?s=otp&amp;t=users&amp;sync=1&amp;user='.$user.'&amp;eid='.$eid;

		echo '<tr '.($i%2==1?'class="list1"':'class="list2"').'>
			<td onClick="location = \''.$sync_link.'\'">
				<a href="'.$sync_link.'">'.$user.'</a></td>
			<td onClick="location = \''.$sync_link.'\'">
				<a href="'.$sync_link.'">'.$eid.'</a></td>
			<td style="width: 8px" '.help('list_user_del').'><a href="?s=otp&amp;t=users&amp;del=user&amp;user='.$user.'&amp;do=1" '.confirm('del_user').'><img src="/images/delete.png" border=0></a></td>';
			//<td>byt pinkod | byt token | synkronisera token | radera användare</td></tr>';
	}

	echo '</table><br><br>';

	echo '<table cellspacing="0" style="border: 1px solid #bbb;padding: 0.5em;">';
	echo '<tr><td colspan="3"><b>Skapa ny användare</b></td></tr>';
	echo "<tr><td>användarnamn:&nbsp;</td>
			<td><input class=\"input-box\" type=\"text\" name=\"username\" value=\"\" size=\"8\"></td><td>&nbsp;</td></tr>";
	echo "<tr><td>token / EID:&nbsp;</td>
			<td>
				<select name=\"eid\">";

	foreach($tokens AS $token_eid) {
		if( ! isset($tokens_taken[$token_eid]) )
			echo '<option value="'.$token_eid.'">'.$token_eid.'</option>';
	}

	echo "			</select>
			</td><td>&nbsp;</td></tr>";
	echo "<tr><td>pin-kod:&nbsp;</td>
			<td><input class=\"input-box\" type=\"text\" name=\"pin\" value=\"\" size=\"4\"></td>
			<td><input type=\"submit\" name=\"b_new\" value=\"skapa\" class=\"button\" /></td></tr>";
	echo "</table><br/>";	
}


function showtoken_sync()
{
	global $otppasswdFile;
	global $otpstateDir;

	$user = trim($_GET['user']);
	$eid = trim($_GET['eid']);

	echo '<input type="hidden" name="user" value="'.$user.'">';
	echo '<input type="hidden" name="eid" value="'.$eid.'">';

	echo '<table cellspacing="0"><tr>
		<td class="tbhead" colspan="2">Synkronisering av token med eid:'.$eid.'</td>
		</tr>';

	echo '<tr><td colspan="2"><b>Tryck fram två efterföljande engångskoder</b></td></tr>';
	echo '<tr><td style="width:10em">Engångskod 1:&nbsp;</td><td><input type="text" name="pass1" size="6" value=""></td></tr>';
	echo '<tr><td style="width:10em">Engångskod 2:&nbsp;</td><td><input type="text" name="pass2" size="6" value=""></td></tr>';

	echo '</table><br>';
}


function showtokens()
{
	global $otppasswdFile;
	global $otptokensFile;

	echo "<table cellspacing=\"0\"><tr>
		<td class=\"tbhead\">Token (EID)</td>
		<td class=\"tbhead\">Användare</td>
		<td class=\"tbhead\">&nbsp;</td></tr>";

	$otppasswd_arr = file($otppasswdFile);
	foreach($otppasswd_arr AS $otppasswd_line) {
		preg_match('/^([^:^\s]*):([^:^\s]*):([^:^\s]*)/', $otppasswd_line, $match);
		$users[$match[3]] = $match[1];
	}

	$otptokens_arr = file($otptokensFile);
 	foreach($otptokens_arr AS $otptokens_line) {
		preg_match('/^([^:^\s]*):([^:^\s]*):(.)/', $otptokens_line, $match);
		echo '<tr>
			<td>'.$match[1].'</td>
			<td>'.$users[$match[2]].'</td>
			<td>synkronisera token | markera som borttappad</td></tr>';
	}

	echo '</table>';
	
}


function showimport()
{
	global $otptokensFile;

	
}


function showsettings()
{
	global $radius_clientsFile;

	$clients_conf = file_get_contents($radius_clientsFile);

	preg_match('@^# Uggla wwwadmin\s*client\s*([^/]*)/32\s*\{\s*secret\s*=\s*([^\s]*)\s*shortname\s*=\s*([^\s]*)\s*}@m', $clients_conf, $match );
	$ip = $match[1];
	$key = $match[2];

	echo '<table cellspacing="0">';
	echo '<tr><td class="tbhead" colspan="2">Anslutning till Billion SSL VPN</td></tr>';

	echo '<tr><td>IP-adress:&nbsp;</td><td><input type="text" name="ip" value="'.$ip.'"></td></tr>';
	echo '<tr><td>Lösenord:&nbsp;</td><td><input type="text" name="key" value="'.$key.'"></td></tr>';

	echo '</table><br>';
}


////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////

$tabs = array(
		array('users', 'Användare'), 
		//array('tokens', 'Tokens'),
		//array('import', 'Importera nya tokens'),
		array('settings', 'Inställningar'),
	);

switch( $_GET['t'] ) {
default:
case 'users':
	if( isset($_GET['sync']) ) {
		html_start( array( array('users', 'Synkronisering av token') ) );
		showtoken_sync();
		html_end(true, false, false, 'otp', 'users');
	}
	else {
		html_start($tabs);
		showusers();
		html_end(false, false, false);
	}
	break;
case 'tokens':
	html_start($tabs);
	showtokens();
	html_end();
	break;
case 'import':
	html_start($tabs);
	showimport();
	html_end();
	break;
case 'settings':
	html_start($tabs);
	showsettings();
	html_end(true, true, true);
	break;
}
?>
